package alkaid.eis.schedule.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import alkaid.eis.schedule.component.ScheduleComponent;
import alkaid.eis.schedule.domain.JobInfo;
import alkaid.eis.schedule.domain.JobService;
import lombok.RequiredArgsConstructor;

/**
 * 定时任务接口
 *  
 * @author Lucky Yang
 * @since 0.0.1
 */
@RestController
@RequestMapping("/schedules")
@RequiredArgsConstructor
public class ScheduleController {
    private final ScheduleComponent scheduleComponent;
    private final JobService jobService;

    @GetMapping("/{jobInfoId}/start")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void start(@PathVariable("jobInfoId") Long jobInfoId) {
        JobInfo jobInfo = jobService.findInfoById(jobInfoId);
        scheduleComponent.start(jobInfo);
    }

    @GetMapping("/{jobInfoId}/shutdown")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void shutdown(@PathVariable("jobInfoId") Long jobInfoId) {
        JobInfo jobInfo = jobService.findInfoById(jobInfoId);
        scheduleComponent.shutdown(jobInfo);
    }

    @GetMapping("/shutdown-all")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void shutdownAll() {
        scheduleComponent.shutdownAll();
    }

    @GetMapping("/start-all")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void startAll() {
        scheduleComponent.startAll();
    }
}
